# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

class Star_1():

  def __init__(self, H, D, THETA, X_N, Y_N):
    # 几何形状参数
    self.H = H         # 直杆长度
    self.D = D         # 斜杆在直杆的投影长度
    self.THETA = THETA # 直杆与斜杆夹角

    # 大小规格（阵列数目）
    self.X_N = X_N      
    self.Y_N = Y_N

    # 阵列间距
    self.X_SPACE = 2 * D
    self.Y_SPACE = 2 * (H - D / math.tan(THETA / 180.0 * math.pi))

  # 生成胞元
  def generateCellUnit(self):

    def tan(theta):
      return math.tan(theta / 180.0 * math.pi)

    H = self.H
    D = self.D
    THETA = self.THETA

    # 内凹六边形的六个顶点坐标（从正上方开始编号为1，逆时针编号）
    point_1 = (0, 0.5 * H - D / tan(THETA), 0)
    point_2 = (-D, 0.5 * H, 0)
    point_3 = (-D, -0.5 * H, 0)
    point_4 = (0, -(0.5 * H - D / tan(THETA)), 0)
    point_5 = (D, -0.5 * H, 0)
    point_6 = (D, 0.5 * H, 0)

    # 内凹六边形的六条边
    line_1 = (point_1, point_2)
    line_2 = (point_2, point_3)
    line_3 = (point_3, point_4)
    line_4 = (point_4, point_5)
    line_5 = (point_6, point_5)  #阵列后这里会出现重复线段，需去除
    line_6 = (point_6, point_1)
    
    # 上下两条连杆的顶点坐标
    link_point_1 = (0, H - D / tan(THETA), 0)
    link_point_2 = (0, -(H - D / tan(THETA)), 0)

    # 上下两条连杆
    link_1 = (link_point_1, point_1)
    link_2 = (link_point_2, point_4)

    lines = [line_1, line_2, line_3, line_4, line_5, line_6,
            link_1, link_2]

    return lines

  # 沿x轴、y轴阵列线段
  def arrayCellUnit(self,cellUnitLines):

    X_N = self.X_N
    Y_N = self.Y_N

    X_SPACE = self.X_SPACE
    Y_SPACE = self.Y_SPACE

    X_arrayLines = []
    Y_arrayLines = []

    # 胞元沿x轴进行阵列
    for i in range(X_N):

      for item in cellUnitLines:

        point_x_1 = (item[0][0] + X_SPACE * i, item[0][1], item[0][2])
        point_x_2 = (item[1][0] + X_SPACE * i, item[1][1], item[1][2])
        
        X_arrayLines.append((point_x_1, point_x_2))
    
    # 胞元沿y轴进行阵列
    for j in range(Y_N):

      for each in X_arrayLines:

        point_y_1 = (each[0][0], each[0][1] + Y_SPACE * j, each[0][2])
        point_y_2 = (each[1][0], each[1][1] + Y_SPACE * j, each[1][2])

        Y_arrayLines.append((point_y_1, point_y_2))
    
    # 去掉阵列后重复的线段
    Y_arrayLines = list(set(Y_arrayLines))

    return Y_arrayLines
  
  # 打印参数
  def printParameters(self):

    print(" ")

    print("H: %s " % self.H)
    print("D: %s " % self.D)
    print("THETA: %s " % self.THETA)
    print("X_N: %d " % self.X_N)
    print("Y_N: %d " % self.Y_N)
  
  # 创建Part
  def part(self):

    lines = []

    cellUnitLines = self.generateCellUnit()

    lines = self.arrayCellUnit(cellUnitLines)

    lines_tuple = tuple(lines)

    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=DEFORMABLE_BODY)

    mdb.models['Model-1'].parts['Part-1'].WirePolyLine(mergeType=IMPRINT, meshable=ON, 
        points=lines_tuple)

    mdb.models['Model-1'].parts['Part-1'].Set(edges=
        mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#3 ]', 
        ), ), name='Wire-2-Set-1')
    
    self.printParameters()

def main(H, D, THETA, X_N, Y_N):
  star_1 = Star_1(H, D, THETA, X_N, Y_N)
  star_1.part()

if __name__ == '__main__':

  main(H=10.0,D=3.0,THETA=45.0,X_N=5,Y_N=3)

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
